<template>
  <div>
    <el-card class="box-card">
      <div slot="header">
        <el-form
          :inline="true"
          :model="search"
          size="mini"
          class="demo-form-inline"
        >
          <el-form-item>
            <el-button type="success" @click="createRoles">添加职位</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="danger" @click="deleteRoles">批量删除</el-button>
          </el-form-item>
          <el-form-item>
            <el-input
              v-model="search.name"
              placeholder="请输入需要查询的职位"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="getRolesList">查询</el-button>
          </el-form-item>
        </el-form>
      </div>

      <!-- @selection-change="handleSelectionChange" -->
      <el-table
        :data="roles"
        stripe
        ref="multipleTable"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="RoleID" label="职位编号"> </el-table-column>
        <el-table-column prop="RoleName" label="职位名"> </el-table-column>
        <el-table-column prop="DeptName" label="部门编号"> </el-table-column>
        <el-table-column label="状态">
          <template #default="{ row }">
            <el-switch
              disabled
              v-model="row.Type"
              active-color="red"
              inactive-color="green"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="Remark" label="描述"> </el-table-column>
        <el-table-column prop="LastTime" label="创建时间" width="200">
        </el-table-column>
        <el-table-column label="操作" width="300" fixed="right">
          <template #default="{ row }">
            <el-button type="warning" size="small" @click="updateUser(row)"
              >修改</el-button
            >
            <el-button
              type="primary"
              size="small"
              style="margin-right: 10px; margin-left: 10px"
              @click="shoquan(row)"
              >授权</el-button
            >
            <el-popconfirm
              confirm-button-text="好的"
              cancel-button-text="不用了"
              icon="el-icon-info"
              icon-color="red"
              title="确实删除此用户吗？"
              @confirm="deleteRole(row.RoleID)"
            >
              <el-button type="danger" slot="reference" size="small"
                >删除</el-button
              >
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        style="margin-top: 15px"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-size="size"
        :page-sizes="[5, 10, 20, 30, 40]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="count"
      >
      </el-pagination>

      <el-dialog
        title="添加职位"
        append-to-body
        :visible.sync="createDialogVisible"
        width="450"
      >
        <Create
          :dialogVisible.sync="createDialogVisible"
          @refresh="getRolesList"
        ></Create>
      </el-dialog>

      <el-dialog
        title="授权"
        append-to-body
        :visible.sync="authorizationDialogVisible"
        width="450"
      >
        <Authorization
          :dialogVisible.sync="authorizationDialogVisible"
          :data="role"
          @refresh="getRolesList"
        ></Authorization>
      </el-dialog>

      <el-dialog
        title="修改职位"
        append-to-body
        :visible.sync="editDialogVisible"
        width="450"
      >
        <Edit
          :dialogVisible.sync="editDialogVisible"
          :data="role"
          @refresh="getRolesList"
        ></Edit>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import Create from "./Create.vue";
import Edit from "./Edit.vue";
import Authorization from "./Authorization.vue";

export default {
  name: "",
  data() {
    return {
      //查询搜索
      search: {
        name: "",
      },

      //复选框
      multipleSelection: [],

      //分页列表
      count: 0,
      size: 5,
      page: 1,
      roles: [],

      role: {},

      createDialogVisible: false,
      editDialogVisible: false,
      authorizationDialogVisible: false,
    };
  },
  created() {
    this.getRolesList();
  },
  methods: {
    getRolesList() {
      this.axios({
        method: "get",
        url: "Roles/PageList",
        params: { page: this.page, limit: this.size, name: this.search.name },
        data: {},
      })
        .then((res) => {
          this.count = res.data.count;
          this.roles = res.data.data;
        })
        .catch((err) => {
          console.log("err: ", err);
        });
    },
    //切换每页条数
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.page = 1;
      this.size = val;
      this.getRolesList();
    },
    //翻页
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page = val;
      this.getRolesList();
    },
    //添加用户  弹窗
    createRoles() {
      this.createDialogVisible = true;
    },

    //删除单个数据
    deleteRole(id) {
      this.axios({
        method: "delete",
        url: "Roles/" + id,
      })
        .then((res) => {
          console.log("res: ", res);
          if (res.data.code > 0) {
            this.$message({
              message: res.data.msg,
              type: "success",
            });

            this.getRolesList(); //刷新
          } else {
            this.$message({
              message: res.data.msg,
              type: "error",
            });
          }
        })
        .catch((err) => {
          console.log("err: ", err);
        });
    },
    //批量删除
    handleSelectionChange(val) {
      //保存选中的行数据
      this.multipleSelection = val;
    },
    deleteRoles() {
      let ids = [];
      ids = this.multipleSelection.map((v) => v.RoleID);
      if (ids.length > 0) {
        this.$confirm("此操作将永久删除数据, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            this.axios({
              method: "delete",
              url: "Roles",
              params: {},
              data: ids,
            })
              .then((res) => {
                console.log("res: ", res);
                if (res.data.code > 0) {
                  this.$message({
                    message: res.data.msg,
                    type: "success",
                  });

                  this.getRolesList(); //刷新
                } else {
                  this.$message({
                    message: res.data.msg,
                    type: "error",
                  });
                }
              })
              .catch((err) => {
                console.log("err: ", err);
              });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      } else {
        this.$message({
          message: "请选择需要删除的数据！",
          type: "warning",
        });
      }
    },
    updateUser(row) {
      this.role = row;
      this.editDialogVisible = true;
    },
    shoquan(row) {
      this.role = row;
      this.authorizationDialogVisible = true;
    },
  },
  components: {
    Create,
    Edit,
    Authorization,
  },
};
</script>

<style scoped>
/* .demo-form-inline {
    float: right;
} */

.demo-form-inline .el-form-item {
  margin-bottom: 0px;
}
</style>